package generatTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import DAOImpl.BooksDAOIplm;
import DAOImpl.BooksDAOIplmTest;
import POJO.Books;
import Util.DataProduct;

public class Test {
	public static void main(String[] args) throws IOException {
		
		writeDatabase();
//		writeFile();
	}
	
	public static void writeFile() throws IOException{
		long start = System.currentTimeMillis();
		FileOutputStream fout = null;
		ObjectOutputStream oos =null;
		try {
			fout = new FileOutputStream("D:\\BOOKS.txt");
			oos = new ObjectOutputStream(fout);
			
			for (int i = 0; i < 10000; i++) {
				Books book = DataProduct.product(i);
				oos.writeObject(book);
			}
			oos.writeObject(null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			fout.close();
			oos.close();
		}
		
		long end = System.currentTimeMillis();
		System.out.println("共用时" + (end-start));
	}
	
	public static void writeDatabase(){
		long start = System.currentTimeMillis();
		try {
			
			FileInputStream fin = new FileInputStream("D:\\BOOKS.txt");
			ObjectInputStream oin = new ObjectInputStream(fin);
			BooksDAOIplm bdi = new BooksDAOIplm();
			List<Books> list = new ArrayList<Books>();
			Object obj = null;
			int co=0;
			while((obj = oin.readObject())!=null){
				Books book = (Books) obj;
				co++;
				list.add(book);
				if(list.size()==500){
					bdi.addBooks(list);
					list.clear();
				}
			}
			System.out.println(co);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		long end = System.currentTimeMillis();
		System.out.println("写入数据库用时" + (end-start));
		
		
	}
}
